package com.ics.cmsadmin.frame.config;

import com.ics.cmsadmin.frame.interceptor.ApiLoginInterceptor;
import com.ics.cmsadmin.frame.interceptor.AuthorizeInterceptor;
import com.ics.cmsadmin.frame.interceptor.BeforeLoginInterceptor;
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * Created by Sandwich on 2018-08-13
 */
@Configuration
public class LoginConfiguration implements WebMvcConfigurer {

    @Resource
    private ApiLoginInterceptor apiLoginInterceptor;      // 登陆校验
    @Resource
    private AuthorizeInterceptor authorizeInterceptor;  // 权限校验
    @Resource
    private BeforeLoginInterceptor beforeLoginInterceptor; // 跨域访问

    @Bean
    public MessageSource messageSource(){
        ResourceBundleMessageSource messageSource =
            new ResourceBundleMessageSource();
        messageSource.setBasename("validator/message");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public Validator validator(){
        ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
            .configure()
            .addProperty(HibernateValidatorConfiguration.FAIL_FAST, "false" )
            .messageInterpolator(new ResourceBundleMessageInterpolator(
                new MessageSourceResourceBundleLocator(messageSource())))
            .buildValidatorFactory();
        return validatorFactory.getValidator();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] excludePaths = {
            "/", "/api/**", "/error", "logout", "/oauth2/**",
            // 排除资源请求
            "*.css", "*.js", "*.png","/dist/**","/pay/**",
            // 排除swagger相关路径
            "/swagger-ui.html", "/configuration/*", "/swagger-resources/**", "/v2/api-docs", "/webjars/**"
        };
        // 注册拦截器
        InterceptorRegistration beforeLoginRegistry = registry.addInterceptor(beforeLoginInterceptor);
        beforeLoginRegistry.addPathPatterns("/**");

//        InterceptorRegistration apiLoginRegistry = registry.addInterceptor(apiLoginInterceptor);
//        apiLoginRegistry.addPathPatterns("/api/**");
//        apiLoginRegistry.excludePathPatterns("/api/pay/**");

        InterceptorRegistration authorizeRegistry = registry.addInterceptor(authorizeInterceptor);
        authorizeRegistry.addPathPatterns("/**");
        authorizeRegistry.excludePathPatterns(excludePaths);


    }
}
